//
//  HLMainTabBar.swift
//  MyWeiBo
//
//  Created by fannie on 16/3/19.
//  Copyright © 2016年 fannie. All rights reserved.
//

import UIKit

let count=5

class HLMainTabBar: UITabBar {

    var btnClick:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundImage=UIImage(named: "tabbar_background")
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width=frame.width / CGFloat(count)
        
//        print(subviews)
        var index=0
        let cls=NSClassFromString("UITabBarButton")
        for view in subviews {
            
            if view.isKindOfClass(cls!) {
                view.frame=CGRect(x: CGFloat(index) * width, y: 0, width: width, height: frame.height)
                index += index==1 ? 2 :1
            }
        }
        
        addBtn.frame=CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
    }
    
    private lazy var addBtn:UIButton={
        let btn=UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted@2x"), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: "btnClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(btn)
        
        return btn
    }()
    
    func btnClickAction()->Void {
        btnClick?()
    }

}
